﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace NodeBasedSkillEditor.View
{
    public partial class EnumerateField : FieldBase
    {
        public Enum Value {
            get { return (Enum)GetValue(ValueProperty); }
            set {
                if (value != null && Value != value)
                {
                    var needRefresh = Value == null || Value.GetType() != value.GetType();
                    SetValue(ValueProperty, value);
                    if (needRefresh)
                    {
                        RefreshComboBox(Value);
                    }
                    else
                    {
                        Combo.SelectedItem = Value;
                    }
                }
            }
        }
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(Enum), typeof(EnumerateField),
                new FrameworkPropertyMetadata(
                    null,
                    FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                    OnValuePropertyChanged)
                );
        static void OnValuePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var s = sender as EnumerateField;
            s.InvokeOnValueChanged(e.OldValue, e.NewValue);
        }


        //public SolidColorBrush ArrowColor {
        //    get { return (SolidColorBrush)GetValue(ArrowColorProperty); }
        //    set { SetValue(ArrowColorProperty, value); }
        //}
        //public static readonly DependencyProperty ArrowColorProperty =
        //    DependencyProperty.Register("ArrowColor", typeof(SolidColorBrush), typeof(EnumerateField),
        //        new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Black), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));



        public EnumerateField()
        {
            InitializeComponent();
            Combo.SelectionChanged += OnEnumSelect;
        }

        public EnumerateField(Enum value)
        {
            InitializeComponent();
            Combo.SelectionChanged += OnEnumSelect;
            RefreshComboBox(Value);

            Value = value;
        }

        private void OnEnumSelect(object sender, SelectionChangedEventArgs e)
        {
            //ComboBox box = sender as ComboBox;
            Value = Combo.SelectedItem as Enum;
        }

        public void RefreshComboBox(Enum v)
        {
            if (v != null)
            {
                Combo.ItemsSource = Enum.GetValues(v.GetType());
                Combo.SelectedItem = v;
            }
        }

    }
}
